/**
 * Demo class
 * @coding: utf-8
 * @author: phinix.chen@gmail.com
 * @see: http://code.google.com/intl/zh-CN/apis/maps/documentation/javascript/reference.html#PolylineOptions
 * @reference: PolylineOptions 属性:strokeOpacity
 * @fileoverview: This JavaScript file lets you to experience one of the features,
 *                classes, methods, options or properties in Google Maps API.
 */


/**
 * Constructor for Demo class.
 * @param {String} filePath
 */
var Demo = function(filePath, docLink){
  var param = this.getParam(filePath);
  this.param = param;
  if (param) {
    this.buildContext(param);
    this.run();
  } else {
    alert('Need essential parameters to run the demo.');
  }
  if(docLink && param['titleId']){
    document.getElementById(param['titleId']).href = docLink;
  }
}

/**
 * Method to build the context that required by the core feature which the Demo
 * class want to show.
 * @param {Object} param
 */
Demo.prototype.buildContext = function(param){
  var options = {
    zoom: parseInt(param['zoom']),
    center: new google.maps.LatLng(parseFloat(param['lat']), parseFloat(param['lng'])),
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  map = new google.maps.Map(document.getElementById(param['mapId']), options);
  this.map = map;
}

/**
 * Invoked by Demo.prototype.run, contain the codes to display on the page, so
 * please use some key words such as this in the method, try pass all necessary
 * parameters by arguments.
 */
Demo.prototype.run = function(){
  if (this.running) {
    this.reset();
  }
  this.kernel();
  this.running = true;
  if (this.param && this.param['codeId']) {
    document.getElementById(this.param['codeId']).innerHTML = this.toString();
  }
}

/**
 * Invoked by Demo.prototype.run, contain the codes to display on the page.
 */
Demo.prototype.kernel = function(){
  // TODO
}

/**
 * Restore the context to the status before the core feature running.
 */
Demo.prototype.reset = function(){
  // TODO
  this.running = null;
  if (this.param && this.param['codeId']) {
    document.getElementById(this.param['codeId']).innerHTML = '';
  }
}

/**
 * Return the string of the demo codes of the core feature related in this Demo
 * class. The codes should be run directly in other pages as demo without any
 * modification.
 * @return {String} Return codes string.
 */
Demo.prototype.toString = function(){
  return '<h3 style="color:red">Not implement yet!</h3>'
  var funcPatn = /function\s+\([^\)]*\)\s*{.*/g;
  var returnPtn = /return\s+[^;]*;/g
  return Demo.prototype.kernel.toString().replace(funcPatn, '').replace(/}$/, '').replace(returnPtn, '');
}

/**
 * Get parameters from the file path of this Demo file, and the path is the value
 * of the script src attribute in the script tag that import this file.
 * @param {String} filePath
 */
Demo.prototype.getParam = function(filePath){
  var exp = new RegExp(filePath);
  var scripts = document.getElementsByTagName("script");
  for (var i = scripts.length - 1; i >= 0; i--) {
    if (exp.test(scripts[i].src)) {
      var param = {};
      var path = scripts[i].src;
      var result = path.match(/([^=^\?^&]+)=([^&^#]+)/g);
      for (var i = result.length - 1; i >= 0; i--) {
        arr = result[i].split('=')
        param[arr[0]] = arr[1]
      }
      return param;
    }
  }
  return null;
}

/**
 * Create the instance of Demo class and run the demo.
 */    
    
var demo = new Demo('polylineoptions/strokeopacity.js', 'http://code.google.com/intl/zh-CN/apis/maps/documentation/javascript/reference.html#PolylineOptions');